<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DB_UTIL
 *
 * @author Rene
 */
class DB_UTIL 
{
    private $con = null;
    private $db = null;
    
    public function __construct() 
    {
        $this->startCon();
    }
    
    public function __destruct()
    {
        $this->CloseDB();
    }    
    
    private function startCon()
    {
        $this->con = mysql_connect(Config::$DB_LOCATION, 
                                Config::$DB_USER,
                                Config::$DB_PASS);
        
        if(!$this->con)
        {
            $error = "Connect to DB failed<br />";
            
            if(Config::$DEBUG == true)
            {
                $error = $error . 
                        "DB_LOCATION: " . 
                        Config::$DB_LOCATION .
                        "<br />" .
                        "DB_USER: " .
                        Config::$DB_USER .
                        "<br />" .
                        "DB_PASS: " .
                        Config::$DB_PASS .
                        "<br />" .
                        "DB_NAME: " .
                        Config::$DB_NAME .
                        "<br />" . 
                        mysql_errno();
            }
            
            die($error);
        }
        
        mysql_query("SET NAMES 'utf8'", $this->con);
        
        $db = mysql_select_db(Config::$DB_NAME, $this->con);
        
        if(!$db)
        {
            $error = "Select DB failed.";
            
            if(Config::$DEBUG == true)
            {
                $error = $error .
                        "<br />" .
                        mysql_errno();
            }
            
            die($error);
        }
    }
    
    private function CloseDB()
    {
        if(isset($this->con) && $this->con)
        {
            mysql_close($this->con);
        }
    }
    
    
    public function ExecSql($inSql)
    {
        if(!isset($inSql))
            return;
        
        return mysql_query($inSql, $this->con);
    }
    
    public function ExecSqlLocked($inSql, $access = "WRITE")
    {
        if(!isset($inSql))
            return;
        
        $this->CreateLockedTransaction($access);
        $result = mysql_query($inSql, $this->con);
        
        if($result)
        {
            $this->CommitLockedTransaction();
        }
        else
        {
            $this->RollbackLockedTransaction();
        }
        
        return $result;
    }
    
    
    public function InsertLocked($inSql, $access = "WRITE")
    {
        if(!isset($inSql))
            return;
        
        $this->CreateLockedTransaction($access);
        $result = mysql_query($inSql, $this->con);
        
        if($result)
        {
            $id = mysql_insert_id();
            $this->CommitLockedTransaction();
        }
        else
        {
            $this->RollbackLockedTransaction();
        }
        
        if($result)
            return $id;
        else
            return false;
    }


    private function CreateLockedTransaction($access)
    {
        $this->ExecSql("LOCK TABLES table $access;");
        $this->ExecSql("SET AUTOCOMMIT=0");
        $this->ExecSql("START TRANSACTION");
    }
    
    private function CommitLockedTransaction()
    {
        $this->ExecSql("COMMIT");
        $this->ExecSql("UNLOCK TABLES;");
        $this->ExecSql("SET AUTOCOMMIT=1");
    }
    
    private function RollbackLockedTransaction()
    {
        $this->ExecSql("ROLLBACK");
        $this->ExecSql("UNLOCK TABLES;");
        $this->ExecSql("SET AUTOCOMMIT=1");
    }
}

?>
